/**
 * Filename: MusicAlbumAdapter.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/5/16 16:12
 */
package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.YNMusicWebActivity;
import com.github.postsanf.yinian.bean.YNMusicAlbum;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.YNUrlHelper;
import com.github.postsanf.yinian.view.CircleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.imageaware.ImageViewAware;
import java.util.List;

public class MusicAlbumAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int EMPTY_VIEW = 10;
    private Context mContext;
    private List<YNMusicAlbum> datas;
    private int groupid;

    public MusicAlbumAdapter(Context mContext, List<YNMusicAlbum> datas,int groupid) {
        this.mContext = mContext;
        this.datas = datas;
        this.groupid = groupid;
    }

    public class EmptyViewHolder extends RecyclerView.ViewHolder {
        public EmptyViewHolder(View itemView) {
            super(itemView);
        }
    }

    public  class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout        ll_item_music_album;					        // 列表View
        CircleImageView     music_status_user_avatar;					    // 头像
        TextView            music_status_user_name;					        // 昵称
        TextView            music_status_time;                              // 发布时间
        SimpleDraweeView    iv_music_status_pic;                            // 封面图
        ImageView           iv_music_status_share;                          // 分享按钮

        public ViewHolder(View itemView) {
            super(itemView);
            ll_item_music_album	= (LinearLayout) itemView
                    .findViewById(R.id.ll_item_music_album);
            music_status_user_avatar = (CircleImageView) itemView
                    .findViewById(R.id.music_status_user_avatar);
            music_status_user_name = (TextView) itemView
                    .findViewById(R.id.music_status_user_name);
            music_status_time = (TextView) itemView
                    .findViewById(R.id.music_status_time);
            iv_music_status_pic = (SimpleDraweeView) itemView
                    .findViewById(R.id.iv_music_status_pic);
            iv_music_status_share = (ImageView) itemView
                    .findViewById(R.id.iv_music_status_share);
        }

        public void bindData(final YNMusicAlbum listItems)
        {
            if (listItems != null)
            {
                String paths = YNUrlHelper.getHomeImgUrl(mContext, listItems.getMaCover());
                Uri uri = Uri.parse(paths);
                ImageOptHelper.showFrescoHome(uri, iv_music_status_pic, mContext);
                ImageLoader.getInstance().displayImage(listItems.getUserInfo().getUpic(), new ImageViewAware(music_status_user_avatar, false), ImageOptHelper.getAvatarOptions());
                music_status_user_name.setText(listItems.getUserInfo().getUnickname());
                music_status_time.setText(listItems.getMaTime());
                // 点击列表
                ll_item_music_album.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, YNMusicWebActivity.class);
                        intent.putExtra(ACTIONs.aUrl, listItems.getTempletUrl());
                        intent.putExtra(ACTIONs.aGroupid, groupid);
                        intent.putExtra(ACTIONs.aEid,listItems.getMaID());
                        intent.putExtra(ACTIONs.aTitle, listItems.getMaName());
                        mContext.startActivity(intent);
                    }
                });
            }
        }
    }

    private YNMusicAlbum getItem(int position) {
        return datas.get(position);
    }

    @Override
    public int getItemViewType(int position) {
        if (datas.size() == 0) {
            return EMPTY_VIEW;
        }
        return super.getItemViewType(position);
    }

    // 创建ViewHolder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == EMPTY_VIEW) {
            int resId = R.layout.yn_empty_music_status;
            View v = LayoutInflater.from(parent.getContext()).inflate(resId, parent, false);
            v.getRootView().getLayoutParams().height = DisplayUtils.getScreenHeightPixels((Activity) mContext)-DisplayUtils.dp2px(mContext,210);
            return new EmptyViewHolder(v);
        }
        final View convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.yn_item_music_album, parent, false);
        return new ViewHolder(convertView);
    }

    // 绑定数据到View
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder rv_holder, int position) {
        if (rv_holder instanceof ViewHolder) {
            final ViewHolder holder = (ViewHolder) rv_holder;
            YNMusicAlbum listItems = getItem(position);
            holder.bindData(listItems);
            holder.itemView.setTag(listItems);
        }
    }
    @Override
    public int getItemCount() {
        return datas.size() > 0 ? datas.size() : 1 ;
    }
}
